package com.zy.mc_module.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.core.graphics.drawable.DrawableCompat;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatTextView;

import android.util.AttributeSet;

import com.zy.mc_module.R;

/**
 * Created on 18/11/29
 * TextView 兼容5.0以下矢量图
 */
public class CustomTextView extends AppCompatTextView {
    private Drawable mDrawableLeft;//左
    private Drawable mDrawableTop;//上
    private Drawable mDrawableRight;//右
    private Drawable mDrawableBottom;//下
    private boolean isAliganCenter = true;
    private boolean isMargin = false;
    private int mWidth;
    private Context context;

    public CustomTextView(Context context) {
        super(context);
        this.context = context;
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        this.context = context;
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
        this.context = context;
    }

    public void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        if (null != typedArray) {
            //兼容5.0以下矢量图
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.0以上
                mDrawableLeft = typedArray.getDrawable(R.styleable.CustomTextView_drawable_left);
                mDrawableTop = typedArray.getDrawable(R.styleable.CustomTextView_drawable_top);
                mDrawableRight = typedArray.getDrawable(R.styleable.CustomTextView_drawable_right);
                mDrawableBottom = typedArray.getDrawable(R.styleable.CustomTextView_drawable_bottom);
            } else {
                //5.0以下
                int leftId = typedArray.getResourceId(R.styleable.CustomTextView_drawable_left, -1);
                int topId = typedArray.getResourceId(R.styleable.CustomTextView_drawable_top, -1);
                int rightId = typedArray.getResourceId(R.styleable.CustomTextView_drawable_right, -1);
                int bottomId = typedArray.getResourceId(R.styleable.CustomTextView_drawable_bottom, -1);
                if (leftId != -1) {
                    mDrawableLeft = AppCompatResources.getDrawable(context, leftId);
                }
                if (topId != -1) {
                    mDrawableTop = AppCompatResources.getDrawable(context, topId);
                }
                if (rightId != -1) {
                    mDrawableRight = AppCompatResources.getDrawable(context, rightId);
                }
                if (bottomId != -1) {
                    mDrawableBottom = AppCompatResources.getDrawable(context, bottomId);
                }
            }
            isMargin = typedArray.getBoolean(R.styleable.CustomTextView_isMargin, false);
            isAliganCenter = typedArray.getBoolean(R.styleable.CustomTextView_isAliganCenter, true);
            //图片颜色
            int color = typedArray.getInt(R.styleable.CustomTextView_drawable_color, 0);
            if (color != 0) {
                if (null != mDrawableLeft) {
                    Drawable wrappedDrawable = DrawableCompat.wrap(mDrawableLeft).mutate();
                    DrawableCompat.setTint(wrappedDrawable, color);
                } else if (null != mDrawableTop) {
                    Drawable wrappedDrawable = DrawableCompat.wrap(mDrawableTop).mutate();
                    DrawableCompat.setTint(wrappedDrawable, color);
                } else if (null != mDrawableRight) {
                    Drawable wrappedDrawable = DrawableCompat.wrap(mDrawableRight).mutate();
                    DrawableCompat.setTint(wrappedDrawable, color);
                } else if (null != mDrawableBottom) {
                    Drawable wrappedDrawable = DrawableCompat.wrap(mDrawableBottom).mutate();
                    DrawableCompat.setTint(wrappedDrawable, color);
                }
            }
            typedArray.recycle();
        }
        if (null != mDrawableLeft) {
            this.setCompoundDrawablesWithIntrinsicBounds(mDrawableLeft, null, null, null);
        }
        if (null != mDrawableTop) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, mDrawableTop, null, null);
        }
        if (null != mDrawableRight) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, mDrawableRight, null);
        }
        if (null != mDrawableBottom) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, mDrawableBottom);
        }
    }

    public void setDrawableLeft(int drawableId) {
        if (0 == drawableId) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mDrawableLeft = getResources().getDrawable(drawableId);
            } else {
                mDrawableLeft = AppCompatResources.getDrawable(context, drawableId);
            }
            this.setCompoundDrawablesWithIntrinsicBounds(mDrawableLeft, null, null, null);
        }
    }

    public void setDrawableAndColorLeft(int drawableId, int color) {
        if (0 == drawableId) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mDrawableLeft = getResources().getDrawable(drawableId);
            } else {
                mDrawableLeft = AppCompatResources.getDrawable(context, drawableId);
            }
            Drawable wrappedDrawable = DrawableCompat.wrap(mDrawableLeft).mutate();
            DrawableCompat.setTint(wrappedDrawable, color);
            this.setCompoundDrawablesWithIntrinsicBounds(wrappedDrawable, null, null, null);
        }
    }

    public void setDrawableRight(int drawableId) {
        if (0 == drawableId) {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mDrawableRight = getResources().getDrawable(drawableId);
            } else {
                mDrawableRight = AppCompatResources.getDrawable(context, drawableId);
            }
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, mDrawableRight, null);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        Drawable[] drawables = getCompoundDrawables();
        Drawable drawableLeft = drawables[0];
        Drawable drawableTop = drawables[1];
        Drawable drawableRight = drawables[2];
        Drawable drawableBottom = drawables[3];
        if (drawableLeft != null) {
            setDrawable(drawableLeft, 0);
        }
        if (drawableTop != null) {
            setDrawable(drawableTop, 1);
        }
        if (drawableRight != null) {
            setDrawable(drawableRight, 2);
        }
        if (drawableBottom != null) {
            setDrawable(drawableBottom, 3);
        }
        this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }

    private void setDrawable(Drawable drawable, int tag) {
        //获取图片实际长宽
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        int left = 0, top = 0, right = 0, bottom = 0;
        switch (tag) {
            case 0:
            case 2:
                left = 0;
                top = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
                right = width;
                bottom = top + height;
                break;
            case 1:
                left = isAliganCenter ? 0 : -mWidth / 2 + width / 2;
                top = 0;
                right = left + width;
                bottom = top + height;
                break;
        }
        drawable.setBounds(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isMargin) {
            try {
                //获取左边的图片
                Drawable drawableLeft = getCompoundDrawables()[0];
                if (drawableLeft != null) {
                    //取得字符串的宽度值
                    float textWidth = getPaint().measureText(getText().toString());
                    //获取控件的内边距
                    int drawablePadding = getCompoundDrawablePadding();
                    int drawableWidth;
                    //返回图片呢的固有宽度,单位是DP
                    drawableWidth = drawableLeft.getIntrinsicWidth();
                    float bodyWidth = textWidth + drawableWidth + drawablePadding;
                    canvas.translate((getWidth() - bodyWidth) / 2, 0);
                }
                Drawable drawableRight = getCompoundDrawables()[2];
                if (drawableRight != null) {
                    float textWidth = getPaint().measureText(getText().toString());
                    int drawablePadding = getCompoundDrawablePadding();
                    int drawableWidth = 0;
                    drawableWidth = drawableRight.getIntrinsicWidth();
                    float bodyWidth = textWidth + drawableWidth + drawablePadding;
                    setPadding(0, 0, (int) (getWidth() - bodyWidth), 0);
                    canvas.translate((getWidth() - bodyWidth) / 2, 0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onDraw(canvas);
    }
}
